package divestoclimb.checklist;

import java.io.File;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class D2cChecklist extends ListActivity {

	// Some application-wide parameters
	public static final String TEMPLATE_EXTENSION = "cklst";
	public static final String CHECKLIST_EXTENSION = "cklst";
	public static final File CHECKLIST_DEFAULT_LOCATION = new File(Environment.getExternalStorageDirectory(), "checklists");
	public static final File TEMPLATE_DEFAULT_LOCATION = new File(CHECKLIST_DEFAULT_LOCATION, "templates");
	
	public static final int CODE_BACKUP = 2;
	public static final int CODE_RESTORE = 3;
	
	public static final int RESULT_FAILED = RESULT_FIRST_USER + 1;
	
	/**
	 * Called when the activity is first created.
	 */
	// TODO: ListViews are boring. Redo this screen as a 2x2 grid where each cell
	// has an icon, the action text is in a larger font and there's a description
	// underneath.
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		String main_options[] = {
				getResources().getString(R.string.templates),
				getResources().getString(R.string.new_checklist),
				getResources().getString(R.string.open_checklists),
				getResources().getString(R.string.completed_checklists)
		};
		
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, main_options));
		getListView().setTextFilterEnabled(true);
	}
	
	protected void showTemplateList(int template_list_action) {
		Intent i = new Intent(this, TemplateList.class);
		i.putExtra(TemplateList.KEY_ONCLICK_ACTION, template_list_action);
		startActivity(i);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.about:
			startActivity(new Intent(this, About.class));
			break;
		case R.id.backup:
			startActivityForResult(new Intent(this, Backup.class), CODE_BACKUP);
			break;
		case R.id.restore:
			startActivityForResult(new Intent(this, Restore.class), CODE_RESTORE);
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch(requestCode) {
		case CODE_BACKUP:
			Toast.makeText(this, resultCode == RESULT_OK? R.string.backup_successful: R.string.backup_failure, Toast.LENGTH_SHORT).show();
			break;
		case CODE_RESTORE:
			Toast.makeText(this, resultCode == RESULT_OK? R.string.restore_successful: R.string.restore_failure, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent i;
		if(position >= 0) {
			switch(position) {
			case 0:
				showTemplateList(TemplateList.ACTION_EDIT);
				break;
			case 1:
				showTemplateList(TemplateList.ACTION_NEW_CHECKLIST);
				break;
			case 2:
				i = new Intent(this, ChecklistList.class);
				i.putExtra("completed", false);
				startActivity(i);
				break;
			case 3:
				// TODO: call an activity to show a ListView of time ranges
				i = new Intent(this, ChecklistList.class);
				i.putExtra("completed", true);
				startActivity(i);
				break;
			}
		}
	}
	
}